package org.smth.search.parser.impl;

import com.google.inject.Singleton;

import org.smth.search.parser.ContentParser;
import org.smth.search.types.Post;
import org.smth.search.types.PostInfo;
import org.smth.search.types.impl.PostInfoImpl;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Singleton
public class ContentParserImpl implements ContentParser {

	private String tok1 = "发信人: ";

	private String tok2 = "标  题: ";

	private String tok3 = "发信站: ";

	private String reg1 = "\n^【 在 .*的大作中提到: 】$";

	private Pattern pattern1 = Pattern.compile(reg1, Pattern.MULTILINE);

	private String reg2 = "\n^:.*$";

	private Pattern pattern2 = Pattern.compile(reg2, Pattern.MULTILINE);

	private String reg3 = "※\\s来源:.*\\[FROM:\\s(.{0,24})\\]";

	private Pattern pattern3 = Pattern.compile(reg3);

	private String reg4 = "※\\s修改:·(\\w*)\\s于\\s(.*)\\s修改本文·\\[FROM:\\s(.{0,24})\\]";

	private Pattern pattern4 = Pattern.compile(reg4);

	private String reg5 = ".*\\((.*)\\).*";

	private Pattern pattern5 = Pattern.compile(reg5);

	public String decodeString(String content) {
		if (content == null) {
			return content;
		}
		StringBuffer buffer = new StringBuffer();
		int n = content.length();
		out: for (int i = 0; i < n; i++) {
			char c = content.charAt(i);
			if (c == '\\') {
				if (i < n - 1) {
					char cc = content.charAt(i + 1);
					if (cc == '\\') {
						buffer.append('\\');
						i++;
						continue;
					} else if (cc == 'n') {
						buffer.append('\n');
						i++;
						continue;
					} else if (cc == '/') {
						buffer.append('/');
						i++;
						continue;
					} else if (cc == '\'') {
						buffer.append('\'');
						i++;
						continue;
					} else if (cc == 'r') {
						if (i < n - 2) {
							char ccc = content.charAt(i + 2);
							if (ccc == '[') {
								int now = i + 3;
								while (now < n) {
									ccc = content.charAt(now);
									if (ccc >= 'a' && ccc <= 'z') {
										i = now;
										continue out;
									}
									now++;
								}
							}
						}
					}
				}
			}
			buffer.append(c);
		}
		content = buffer.toString();
		return content;
	}

	public PostInfo parsePostInfo(Post post, String content) {
		PostInfoImpl info = new PostInfoImpl();
		//去掉头部
		if (content.startsWith(tok1)) {
			int index = content.indexOf('\n');
			if (index > 0) {
				content = content.substring(index + 1);
				if (content.startsWith(tok2)) {
					index = content.indexOf('\n');
					if (index > 0) {
						content = content.substring(index + 1);
						if (content.startsWith(tok3)) {
							index = content.indexOf('\n');
							if (index > 0) {
								String one = content.substring(0, index);
								Matcher matcher = pattern5.matcher(one);
								if (matcher.find()) {
									info.setPostTime(matcher.group(1));
								}
								content = content.substring(index + 1);
							}
						}
					}
				}
			}
		}
		//解析尾部
		int index = content.lastIndexOf('※');
		if (index >= 0) {
			//发文ip
			int n = content.indexOf('\n', index);
			String one;
			if (n > 0) {
				one = content.substring(index, n);
			} else {
				one = content.substring(index);
			}
			Matcher matcher = pattern3.matcher(one);
			if (matcher.find()) {
				info.setFromIp(matcher.group(1));
				content = content.substring(0, index);
			}
		}
		index = content.lastIndexOf('※');
		if (index >= 0) {
			//修改
			int n = content.indexOf('\n', index);
			String one;
			if (n > 0) {
				one = content.substring(index, n);
			} else {
				one = content.substring(index);
			}
			Matcher matcher = pattern4.matcher(one);
			if (matcher.find()) {
				info.setModifiedBy(matcher.group(1));
				info.setModifiedAt(matcher.group(2));
				info.setModifiedIp(matcher.group(3));
				content = content.substring(0, index);
			}
		}
		//提出尾巴
		index = content.lastIndexOf("\n--\n");
		if (index >= 0) {
			//签名档
			String tail = content.substring(index + 3);
			content = content.substring(0, index + 1);
			info.setSign(trimFirstReturn(tail));
		}
		info.setMainContent(trimFirstReturn(content)); //正文
		if (post == null || post.getId() != post.getFirstId()) { //是回复
			//尝试去掉引文
			Matcher matcher = pattern1.matcher(content);
			content = matcher.replaceAll("");
			matcher = pattern2.matcher(content);
			content = matcher.replaceAll("");
			info.setPostContent(trimFirstReturn(content));
		}
		return info;
	}

	private String trimFirstReturn(String content) {
		String main;
		if (content.length() > 0 && content.charAt(0) == '\n') {
			main = content.substring(1);
		} else {
			main = content;
		}
		int len = main.length();
		if (len > 0 && main.charAt(len - 1) == '\n') {
			main = main.substring(0, len - 1);
		}
		return main;
	}
}
